<?php
/**
 * Announcements
 *
 * ACCESS: PUBLIC READS, ADMINS/XALT WRITE/EDIT
 *
 * @author Chris Lunsford
 * @version 1.0
 */
class News extends Controller {
	
	/**
	 * Class constructor
	 */
	function __construct() {
		parent::__construct();
		
		$this->badger->checkRemember();
		
		$this->load->model('UserModel');
		$this->load->model('ContentModel');
	}
	
	/******************** URL Functions ********************/
	
	/**
	 * Display the last five posts in full
	 */
	function index() {
		$data = $this->_buildContent();
		$data['pagetype'] = 'nobg';
		unset($data['sidebar']);
		$announcements = $this->ContentModel->getCurrentAnnouncements();
		foreach($announcements->result() as $row) {
			$row->author = $this->UserModel->getUser($row->authorid)->display;
			$row->created = date('F j, Y - g:ia', $row->created);
		}
		
		$data['announcements'] = $announcements;
		$this->load->view('news/announcements', $data);
	}
	
	/**
	 * Displays page: This is the write page for composing an announcement
	 */
	function publish() {
		//Only Admins and XALT can publish for now
		$this->badger->checkAllow('admin xalt');
		
		$this->load->library('validation');
		
		$rules['title'] = "trim|required|max_length[255]|prep_for_form|xss_clean";
		$rules['body'] = "trim|required";
		$this->validation->set_rules($rules);
		
		$fields['title']	= 'Title';
		$fields['body']	= 'Announcement';
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		
		if($this->validation->run() == FALSE) {
			$data = $this->_buildContent();
			$this->load->view('news/news_publish',$data);
		} else {
			$post['title'] = $this->input->post('title', TRUE);
			$post['body'] = $this->input->post('body', TRUE);
			$post['authorid'] = $this->UserModel->getUserID($this->session->userdata('username'));
			$post['created'] = time() + 3600; //add an hour to offset for server time
			$post['expires'] = strtotime($this->input->post('expires').' 23:59:59');
		
			$this->ContentModel->createAnnouncement($post);
			redirect('news');
		}
	}
	
	/**
	 * Displays page: Edit an announcement
	 */
	function editAnnouncement() {
		//Only Admins and XALT can publish for now
		$this->badger->checkAllow('admin xalt');
		$data = $this->_buildContent();
		$data['announcement'] = $this->ContentModel->getAnnouncement($this->uri->segment(3));
		$this->load->view('news/news_edit', $data);
	}
	
	/******************* Task Functions *******************/
	
	/**
	 * Edits an announcement with submitted information
	 */
	function doEditAnnouncement() {
		$this->badger->checkAllow('admin xalt');
		$post['title'] = $this->input->post('title', TRUE);
		$post['body'] = $this->input->post('body', TRUE);
		$post['expires'] = strtotime($this->input->post('expires').' 23:59:59');
		$id = $this->input->post('id');
			
		$this->ContentModel->updateAnnouncement($id,$post);
		redirect('news');
	}
	
	/**
	 * Deletes an announcement from the database
	 */
	function deleteAnnouncement() {
		$this->badger->checkAllow('admin');
		$this->ContentModel->deleteAnnouncement($this->uri->segment(3));
		redirect('admin/news');
	}
	
	/*
	 * Supplies user-specific information to the interface for customization
	 */
	function _buildContent() {
		//REQUIRED//
		$data = array();
		if ($this->session->flashdata('msg_info')) $data['msg_info'] = $this->session->flashdata('msg_info');
		if ($this->session->flashdata('msg_error')) $data['msg_error'] = $this->session->flashdata('msg_error');
		
		//test if user is logged in for site navigation
		if ($this->session->userdata('loggedin')) {
			//grab the user
			$user = $this->UserModel->getUserByName($this->session->userdata('username'));
			
			$data['loggedin'] = TRUE;
			$data['con_user'] = $user;
			$data['sidebar'] = TRUE;
			
			//is admin?
			if ($this->UserModel->isUserInGroup($user->userid,$this->UserModel->getGroupID('admin'))) {
				$data['admin_tab'] = TRUE;
			}
			
			//is prayer team?
			if ($this->UserModel->isUserInGroup($user->userid,$this->UserModel->getGroupID('lg_pop'))) {
				$data['prayer_tab'] = TRUE;
			}
		}
		
		return $data;
	}
}
?>